package com.dc.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dao.SysRoleDeptDao;
import com.dc.project.system.dto.SysRoleDeptDto;
import com.dc.project.system.entity.SysRoleDept;
import com.dc.project.system.service.SysRoleDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色部门关联表(SysRoleDept)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-04 17:09:07
 */
@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptDao, SysRoleDept> implements SysRoleDeptService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询角色部门关联表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(SysRoleDeptDto dto) {
        QueryWrapper<SysRoleDept> queryWrapper = new QueryWrapper<>();
        IPage<SysRoleDept> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增角色部门关联表
     *
     * @param sysRoleDept
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SysRoleDept sysRoleDept) {
        return ResultVo.verify(this.save(sysRoleDept));
    }

    /**
     * 编辑角色部门关联表
     *
     * @param sysRoleDept
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SysRoleDept sysRoleDept) {
        return ResultVo.verify(this.updateById(sysRoleDept));
    }

    /**
     * 根据主键删除角色部门关联表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 根据角色id、部门id批量新增
     *
     * @param roleId
     * @param deptIds
     * @return
     */
    @Override
    public boolean addBatchRoleDept(Integer roleId, List<Integer> deptIds) {
        LambdaQueryWrapper<SysRoleDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleDept::getRoleId, roleId);
        this.remove(queryWrapper);
        List<SysRoleDept> roleDeptList = deptIds.stream().map(deptId -> {
            SysRoleDept roleDept = new SysRoleDept();
            roleDept.setDeptId(deptId);
            roleDept.setRoleId(roleId);
            return roleDept;
        }).collect(Collectors.toList());
        return this.saveBatch(roleDeptList, roleDeptList.size());
    }

    /**
     * 根据角色id获取部门id
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getDeptIdByRoleId(Integer roleId) {
        return baseMapper.getDeptIdByRoleId(roleId);
    }
}